package com.nana.petadventure.pe
{
	import org.cove.ape.APEVector;
	import org.cove.ape.AbstractParticle;
	import org.cove.ape.CircleParticle;
	import org.cove.ape.CollisionEvent;
	import org.cove.ape.Group;
	
	public class ObstacleGroup extends Group
	{
		// 每次添加障碍物时需要用到该值初始化障碍物速度； 引擎的observerSpeed改变时需要调用这里的set方法
		private var _observerSpeed:Number;
		
		public function ObstacleGroup(collideInternal:Boolean=false)
		{
			super(collideInternal);
		}
		
		public function set observerSpeed(value:Number):void
		{
			_observerSpeed = value;
			for each (var obstacle:ObstacleParticle in this.particles) {
				obstacle.velocity = new APEVector(-_observerSpeed,0);
			}
		}

		// 生成一个障碍物
		public function addObstacle():void {
			var ob:ObstacleParticle = new ObstacleParticle(960, Math.random() * 600+20,5,false,0.1,0);
			//ob.setDisplay(new Obstacle());
			//ob.sprite.cacheAsBitmap = true;
			this.addParticle(ob);
			ob.velocity = new APEVector(-_observerSpeed,0);
		}
		
		// 定时回收: 每隔一段时间执行一次 不需要每帧都执行
		public function dismissObstacles():void {
			for each (var obstacle:ObstacleParticle in this.particles) {
				if(obstacle.shouldDismiss == true) {
					this.removeParticle(obstacle);
				}
			}
		}
		
		// 撞击处理
		public function handleCrash(target:ObstacleParticle):void {
			this.removeParticle(target);
			
			trace("obstacle crash!!");
		}
	}
}